package com.dtda.edu.miaosha.common;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dtda.edu.miaosha.common.exception.BaseExcpController;  
/**
 * @author 袁强
 * @version 2019年1月30日 上午11:09:47
 * 类说明  
 */
@Controller
@RequestMapping("/")
public class Sendsms extends BaseExcpController{

private static String Url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
	
	@RequestMapping("/getSendsms")
	@ResponseBody
	public Map<String, String> getSendsms(HttpServletRequest request,String telphone) {
		
		System.out.println("传入的电话为 :"+telphone);
		//定义一个返回值
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("code", "fail");  //默认返回失败
		map.put("message", "短信提交失败,请重新获取或手机号不正确...");
		
		HttpClient client = new HttpClient(); 
		PostMethod method = new PostMethod(Url);

		client.getParams().setContentCharset("GBK");
		method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=GBK");

		int mobile_code = (int)((Math.random()*9+1)*100000);
		
		//String md5Encode = StringUtil.MD5Encode(String.valueOf(mobile_code));  //加密
		//String content1 = new String("您的验证码是：" + md5Encode + "。请不要把验证码泄露给其他人。");
	    
		String content = new String("您的验证码是：" + mobile_code + "。请不要把验证码泄露给其他人。");

		//定义一个session域,将手机号和验证码关联,同时与页面传入的短信验证码进行比对校验
		request.getSession().setAttribute(telphone, String.valueOf(mobile_code));
				
		NameValuePair[] data = {//提交短信
			    new NameValuePair("account", "C50914041"), //查看用户名是登录用户中心->验证码短信->产品总览->APIID
			    new NameValuePair("password", "507aaf5edf1272229cc4a224880c9890"),  //查看密码请登录用户中心->验证码短信->产品总览->APIKEY
			    //new NameValuePair("password", util.StringUtil.MD5Encode("密码")),
			    new NameValuePair("mobile", telphone), 
			    new NameValuePair("content", content),
		};
		method.setRequestBody(data);

		try {
			client.executeMethod(method);
			
			String SubmitResult =method.getResponseBodyAsString();

			//System.out.println(SubmitResult);

			Document doc = DocumentHelper.parseText(SubmitResult);
			Element root = doc.getRootElement();

			String code = root.elementText("code");
			String msg = root.elementText("msg");
			String smsid = root.elementText("smsid");

			System.out.println(code);
			System.out.println(msg);
			System.out.println(smsid);
			
			 if("2".equals(code)){
				System.out.println("短信提交成功");
				map.put("code", "sucess");  
				map.put("message", "短信提交成功,请注意查收!");
			}

		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			// Release connection
			method.releaseConnection();
			/**
			 * public void closeIdleConnections(long idleTimeout) { long maxIdleTime =
			 * System.currentTimeMillis() - idleTimeout; if (idleStartTime <= maxIdleTime) {
			 * httpConnection.close(); } }
			 * 
			 */
			client.getHttpConnectionManager().closeIdleConnections(0);   //设置时间为0,手动关闭连接
		}
		
		return map;
	}
}
